package com.hahaliu.hahaliu.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hahaliu.hahaliu.common.result.Result;
import com.hahaliu.hahaliu.pojo.entity.User;
import com.hahaliu.hahaliu.pojo.vo.SearchCountVO;
import com.hahaliu.hahaliu.pojo.vo.VideoInfoVO;
import com.hahaliu.hahaliu.service.search.SearchService;
import com.hahaliu.hahaliu.service.user.UserService;
import com.hahaliu.hahaliu.service.video.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@RestController
@RequestMapping("/user/search")
@Api(tags = "用户端搜索相关接口")
@Slf4j
public class SearchController {

	@Autowired
	private UserService userService;

	@Autowired
	private VideoService videoService;

	@Autowired
	private SearchService searchService;


	@GetMapping("/count")
	@ApiOperation("获取各种类型相关数据数量")
	public Result<SearchCountVO> getCount(@RequestParam("keyword") String keyword) throws UnsupportedEncodingException {
		keyword = URLDecoder.decode(keyword, "UTF-8");  // 解码经过url传输的字符串

		SearchCountVO count = searchService.getCount(keyword);
		return Result.success(count);
	}

	@GetMapping("/video")
	@ApiOperation("搜索相关已过审视频")
	public Result<IPage<VideoInfoVO>> searchVideos(@RequestParam("keyword") String keyword, @RequestParam("page") Integer page) {
		IPage<VideoInfoVO> videoInfoVOIPage = videoService.searchByNameLike(keyword, page, 30);

		return Result.success(videoInfoVOIPage);
	}

	@GetMapping("/user")
	@ApiOperation("搜索相关用户")
	public Result<IPage<User>> searchUsers(@RequestParam("keyword") String keyword, @RequestParam("page") Integer page) {
		IPage<User> userIPage = userService.searchByNameLike(keyword, page, 30);
		return Result.success(userIPage);
	}
	
}
